package shen.yue.example.one.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import shen.yue.example.one.BaseActivity;
import shen.yue.example.one.R;

/**
 * Created by jiangzhuolei on 2017/8/2.
 */

public class Activity2 extends BaseActivity implements View.OnTouchListener, View.OnClickListener {

    ViewGroup viewGroup;
    private int left,right,top,bottom;

    private int lastX;
    private int lastY;
    private int startX = 0;
    private int startY = 0;
    private int endX = 0;
    private int endY = 0;
    private EditText viewById;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        viewGroup = (ViewGroup) findViewById(R.id.root);


        viewById = (EditText) findViewById(R.id.id_text);
        viewById.setOnTouchListener(this);

    }

    public boolean onTouch(View view, MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG,"ACTION_DOWN");
                left = viewGroup.getLeft();
                right = viewGroup.getRight();
                top = viewGroup.getTop();
                bottom = viewGroup.getBottom();


                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                startX = (int) event.getRawX();
                startY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG,"ACTION_UP");
                endX = (int) event.getRawX();
                endY = (int) event.getRawY();
                Log.i(TAG,"startX:"+startX+"---"+"startY:"+startY+"---"+"endX:"+endX+"---"+"endY:"+endY);
                if(Math.abs(endX-startX)<10&&Math.abs(endY-startY)<10){
                    return false;
                }else {
                    return true;
                }
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.i(TAG,"ACTION_POINTER_DOWN");
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.i(TAG,"ACTION_POINTER_UP");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG,"ACTION_MOVE");
//                sop(view,event);
                int dx =(int)event.getRawX() - lastX;
                int dy =(int)event.getRawY() - lastY;

                int left2 = view.getLeft() + dx;
                int top2 = view.getTop() + dy;
                int right2 = view.getRight() + dx;
                int bottom2 = view.getBottom() + dy;
                if(left2 < left){
                    left2 = left;
                    right2 = left2 + view.getWidth();
                }
                if(right2 > right){
                    right2 = right;
                    left2 = right - view.getWidth();
                }
                if(top2 < top){
                    top2 = top;
                    bottom2 = top2 + view.getHeight();
                }
                if(bottom2 > bottom){
                    bottom2 = bottom;
                    top2 = bottom2 - view.getHeight();
                }
                view.layout(left2, top2, right2, bottom2);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                Log.w(TAG,"lastX:"+lastX);
                Log.w(TAG,"lastY:"+lastY);
                break;
        }
        return true;
    }


    private void sop(View view, MotionEvent event){
        int dx =(int)event.getRawX() - lastX;
        int dy =(int)event.getRawY() - lastY;

        int left2 = view.getLeft() + dx;
        int top2 = view.getTop() + dy;
        int right2 = view.getRight() + dx;
        int bottom2 = view.getBottom() + dy;
        if(left2 < left){
            left2 = left;
            right2 = left2 + view.getWidth();
        }
        if(right2 > right){
            right2 = right;
            left2 = right - view.getWidth();
        }
        if(top2 < top){
            top2 = top;
            bottom2 = top2 + view.getHeight();
        }
        if(bottom2 > bottom){
            bottom2 = bottom;
            top2 = bottom2 - view.getHeight();
        }
        view.layout(left2, top2, right2, bottom2);
        Log.i(TAG,"left2:"+left2+"---"+"top2:"+top2+"---"+"right2:"+right2+"---"+"bottom2:"+bottom2);
    }

    private void sop1(){
        viewById.requestFocus();
        InputMethodManager imm = (InputMethodManager) viewById.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

    @Override
    public void onClick(View view) {
        //sop1();
    }
}
